//
// Created by WenDaojiang on 15/11/20.
// Copyright (c) 2015 文道江. All rights reserved.
//

#import "HybridQRCodeAction.h"


@implementation HybridQRCodeAction{
    ZBarViewController *zbv;
}

// 扫描二维码
- (void)Scan:(id) params{
    if(zbv == nil){
        zbv = [[ZBarViewController alloc] init];
        [zbv setDelegate:self];
    }

    if(params[@"title"]){
        zbv.title = params[@"title"];
    }

    if(params[@"label"]){
        zbv.labIntroudction = [[UILabel alloc] init];
        zbv.labIntroudction.text = params[@"label"];
    }

    if (self.controller.navigationController) {
        self.controller.navigationController.navigationBarHidden = NO;
        [self.controller.navigationController pushViewController:zbv animated:YES];
    }else{
        [self.controller presentViewController:zbv animated:YES completion:nil];
    }
}

// 生成二维码
- (void)Generator:(id) params{
    QRCodeGenerator *generator = [QRCodeGenerator instance];
    generator.margin = [params[@"margin"] integerValue];
    if (!generator.margin){
        generator.margin = 3;
    }
    UIImage *image = [generator qrImageForString:params[@"text"] imageSize: [params[@"size"] floatValue]];
    NSData *data = UIImagePNGRepresentation(image);
    self.resultHandler(YES, [data base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength]);
}

- (void)ParserImage:(id) params{
    NSString *url = params[@"url"];
    if(!url){
        self.resultHandler(NO, @"错误的图片地址!");
    }else{
        self.resultHandler(NO, @"抱歉,此功能暂时不可用!");
    }
    /*NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
    [[[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
        if (error){
            self.callback(NO, [error description]);
        }else{
            //NSLog(@"-- %@", response);
            UIImage *img = [UIImage imageWithData:data];
            NSLog(@"%f,%f", img.size.width, img.size.height);
            if(!img){
                self.callback(NO, @"图片解析错误!");
                return;
            }
            ZBarImage *image = [[ZBarImage alloc] initWithCGImage:[img CGImage] size:img.size];
            if (!image){
                self.callback(NO, @"图片解析错误2!");
                return;
            }

            ZBarImageScanner *scanner = [ZBarImageScanner alloc];
            scanner.enableCache = YES;
            [scanner setSymbology: 0
                           config: ZBAR_CFG_X_DENSITY
                               to: 2];
            [scanner setSymbology: 0
                           config: ZBAR_CFG_Y_DENSITY
                               to: 2];
            NSInteger len = [scanner scanImage:image];
            NSLog(@"qr len . %i, %lu", len, image.dataLength);
            //self.callback(YES, scanner.results);
        }
    }] resume];*/
}


#pragma mark ZBarReaderViewDelegate

// 扫码回调
- (void)readerView:(ZBarReaderView *)readerView didReadSymbols:(ZBarSymbolSet *)symbols fromImage:(UIImage *)image{
    const zbar_symbol_t *symbol = zbar_symbol_set_first_symbol(symbols.zbarSymbolSet);
    NSString *symbolStr = [NSString stringWithUTF8String:zbar_symbol_get_data(symbol)];
    if ([symbolStr canBeConvertedToEncoding:NSShiftJISStringEncoding])
        symbolStr = [NSString stringWithCString:[symbolStr cStringUsingEncoding:NSShiftJISStringEncoding] encoding:NSUTF8StringEncoding];


    self.resultHandler(YES, @{
            @"result": symbolStr
    });
    if (self.controller.navigationController) {
        [self.controller.navigationController popViewControllerAnimated:YES];
    } else {
        [zbv dismissViewControllerAnimated:YES completion:nil];
    }
    if (zbv) {
        zbv = nil;
    }
}

- (void)readerView:(ZBarReaderView *)readerView didStopWithError:(NSError *)error {
    NSLog(@"..... %@", error);
    zbv = nil;
}


@end